package com.find.memories.photoviewer;

import java.util.ArrayList;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;

/**
 * CustomViewPager를 확장한여 구현함.<br>
 * CustomViewPager는 몇몇 get함수를 새로 구현한 걸 제외하고는 android.support.v4.view.ViewPager와 동일하다.
 * @author Jang
 *
 */
public class PhotoViewerPager extends CustomViewPager {
	
	public PhotoViewerPager(Context context) {
		super(context);
	}
	
	public PhotoViewerPager(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	 private boolean mEnableScroll = true;
	    
    /**
     * Horizontal Scroll의 동작여부를 설정한다.
     * @param enable
     */
    public void setEnableScroll(boolean enable) {
        mEnableScroll = enable;
    }
	    
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (!mEnableScroll && getAdapter() != null) {
            PhotoViewerBodyInterface body = ((PhotoViewerPagerAdapter) getAdapter()).getBodyInterface();
            if (body != null) {
                ((FrameLayout)this.getCurrentObject()).dispatchTouchEvent(ev);
                return body.sendTouchEvent(ev);
            }
        }
        if (ev == null || ev.getPointerCount() < 1) {
            return false;
        }
    	return super.dispatchTouchEvent(ev);
    }
    
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (!mEnableScroll) {
            return false;
        }
        
    	return super.onInterceptTouchEvent(ev);
    }
    
    /**
     * 자원 정리 시에 사용한다.
     */
    public void clearAllItem() {
        ArrayList<ItemInfo> list = getPagerItemList();
        if (list != null) {
            for (int i=0;i < list.size(); i++) {
                PhotoViewerBodyInterface bodyInterface = (PhotoViewerBodyInterface) ((FrameLayout)list.get(i).object).getTag();
                if (bodyInterface != null) {
                    bodyInterface.destroyItem();
                }
            }
        }
    }
}
